package Gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.TextField;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import Control.UserLibrarian;
import Logic.LoginInfo;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.table.DefaultTableModel;
import client.ClientHendler;
import client.LibaryMain;
import java.awt.Font;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.Component;
/**
 * this class implements the view subscriber details 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
@SuppressWarnings("unused")
public class Viewsubscriberdetail extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JTextField idtextf;
	public   JPanel writepanel;
	public static JTextField idtextField;
	public static JTextField usernametextField;
	public static JTextField telltextField;
	public static JTextField emailtextField;
	public static Viewsubscriberdetail view;	
	private static JScrollPane scrollPane;
	private static JTable table;
	public Viewsubscriberdetail(){
		setBackground(new Color(153, 204, 255));
				init();
	}
	
	/**
	 * init the view subscriber details window 
	 */
	public void init(){//initil the window
		JPanel showd=new JPanel();
	
		//handel back button//
		
		JButton btnBack = new JButton("Back");
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnBack.setBounds(72, 630, 86, 23);
		btnBack.addActionListener(new java.awt.event.ActionListener() {     
            public void actionPerformed(java.awt.event.ActionEvent ev) {
            	btnback(ev);
            					
			}

			private void btnback(ActionEvent ev) {
				MainScreen.cardLayot.show(MainScreen.card, DirectorWindow.roll);
				
			}
        });
		
		idtextf = new JTextField();
		idtextf.setFont(new Font("Tahoma", Font.BOLD, 14));
		idtextf.setBounds(252, 174, 136, 27);
		idtextf.setColumns(10);
		
		
		JLabel lblEnterSubscriberId = new JLabel("Enter Subscriber Id");
		lblEnterSubscriberId.setBounds(47, 177, 156, 20);
		lblEnterSubscriberId.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		final JButton btnViewLending = new JButton("View Lending");
		btnViewLending.setBounds(575, 471, 194, 37);
		btnViewLending.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnViewLending.setEnabled(false);
		JButton btnViewDetail = new JButton("View Detail");
		btnViewDetail.setBounds(449, 169, 421, 37);
		btnViewDetail.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		btnViewDetail.addActionListener(new java.awt.event.ActionListener() {     // Login action listener 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	btnViewDetailActionPerformed(evt);
            
            }

		private void btnViewDetailActionPerformed(ActionEvent evt) {
			if(!idtextf.getText().equals(""))
			{
				btnViewLending.setEnabled(true);		
			ArrayList<String> Arr=new ArrayList<String>();
            Arr.add("ViewSubscriberDetails");
            String idtext=idtextf.getText();
            int j;
            Arr.add(idtext);
    		LibaryMain.chat.handleMessageFromGui(Arr);
			}
    		else 
    			JOptionPane.showMessageDialog(null, "Please enter Subscriber ID ");
		}
        });
		
		

		
		JButton btnLogOut = new JButton("Logout");
		btnLogOut.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnLogOut.setBounds(785, 632, 122, 21);
		btnLogOut.addActionListener(new java.awt.event.ActionListener() {      
            public void actionPerformed(java.awt.event.ActionEvent ev) {
            	btnLogOut(ev);
            					
			}

			private void btnLogOut(ActionEvent ev) {
				ArrayList<String> Arr=new ArrayList<String>();
				Arr.add("Logout");
				Arr.add("Worker");
				Arr.add(LoginInfo.UserName);
				LibaryMain.chat.handleMessageFromGui(Arr);
				MainScreen.cardLayot.show(MainScreen.card, "Login");
				
			}
        });
		
		JLabel lblSubscriberDetails = new JLabel("Subscriber Details");
		lblSubscriberDetails.setBounds(79, 72, 331, 54);
		lblSubscriberDetails.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 26));
		
		JLabel lblId = new JLabel("Id");
		lblId.setBounds(492, 250, 39, 20);
		lblId.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		JLabel lblUsername = new JLabel("Name");
		lblUsername.setBounds(492, 302, 47, 20);
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		JLabel lblTellphone = new JLabel("Telephone");
		lblTellphone.setBounds(492, 352, 86, 20);
		lblTellphone.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(492, 405, 45, 20);
		lblEmail.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		idtextField = new JTextField();
		idtextField.setFont(new Font("Tahoma", Font.BOLD, 14));
		idtextField.setBounds(642, 246, 207, 29);
		idtextField.setColumns(10);
		
		usernametextField = new JTextField();
		usernametextField.setFont(new Font("Tahoma", Font.BOLD, 14));
		usernametextField.setBounds(642, 298, 207, 29);
		usernametextField.setColumns(10);
		
		telltextField = new JTextField();
		telltextField.setFont(new Font("Tahoma", Font.BOLD, 14));
		telltextField.setBounds(642, 348, 207, 29);
		telltextField.setColumns(10);
		
		emailtextField = new JTextField();
		emailtextField.setFont(new Font("Tahoma", Font.BOLD, 14));
		emailtextField.setBounds(642, 401, 207, 29);
		emailtextField.setColumns(10);
		
		//JButton btnViewLending = new JButton("View Lending");
		
		btnViewLending.addActionListener(new java.awt.event.ActionListener() {     // remove book action listener 
            public void actionPerformed(java.awt.event.ActionEvent ev) {
            	btnViewLending(ev);
            				
			}
        });

	
		Vector<String> headers = new Vector<String>();
        headers.add("ID");
        headers.add("Title");
        headers.add( "Description");
		setLayout(null);
		add(btnBack);
		add(btnLogOut);
		add(lblId);
		add(lblUsername);
		add(lblTellphone);
		add(lblEmail);
		add(emailtextField);
		add(telltextField);
		add(usernametextField);
		add(idtextField);
		add(lblSubscriberDetails);
		add(lblEnterSubscriberId);
		add(idtextf);
		add(btnViewDetail);
		add(btnViewLending);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Viewsubscriberdetail.class.getResource("/Gui/images/user-info-icon.png")));
		label.setBounds(152, 185, 410, 392);
		add(label);
		
		
	}


	protected void btnViewLending(ActionEvent ev) {
		if(!idtextField.getText().equals("")){
		String id=idtextField.getText();
		ArrayList<String> Arr=new ArrayList<String>();
        Arr.add("searchlending");
        Arr.add(id);
        LibaryMain.chat.handleMessageFromGui(Arr);
		}
		else 
			JOptionPane.showMessageDialog(null, "Please enter Subscriber ID ");
		
	}


	public static void viewDeatails(String[] str) {
			idtextField.setText(str[1]);
			usernametextField.setText(str[2]);
			telltextField.setText(str[4]);
			emailtextField.setText(str[3]);
			idtextField.setEditable(false);//false=user loged into system can't change the textfield 
			usernametextField.setEditable(false);
			emailtextField.setEditable(false);
			telltextField.setEditable(false);
			
		
		
	}
	/**
	 * create lending for subscriber 
	 * @param str
	 */
	public static void lend(String[] str) {
		 Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		 

	        for (int i=1; i<=(str.length-3);i=i+3){
	        Vector<Object> row = creatrow(str,i);
	        data.add(row);
	        }
		
			System.out.println(">>>>>>>print"+data);
	        Vector<String> headers = new Vector<String>();
	        headers.add("ID");
	        headers.add("Title");
	        headers.add( "Description");
	        
	        //table = new JTable( data, headers);
	      final JTable table=new JTable( data, headers);
	        telltextField.setText("fsdfsdf");
	        table.addMouseListener(new MouseAdapter()    
	        {   
	        public void mouseClicked(MouseEvent evt)   
	        {   
	        int col = table.getSelectedColumn();   
	        int row = table.getSelectedRow();
	        System.out.println(""+col+row);
	        Object o=((String)table.getValueAt(row, col));
	        System.out.println(""+o);
	        }   
	        }); 
	        

	        table.setFillsViewportHeight(true);
	        table.setRowHeight(20);
	        table.setPreferredScrollableViewportSize(new Dimension(900,150));
	        scrollPane.setViewportView(table);
			//setLayout(groupLayout);
	        //  JScrollPane scrollPane = new JScrollPane(table);
		
	
	
		}
	private static Vector<Object> creatrow(String[] str, int i) {
		 Vector<Object> row = new Vector<Object>();
       row.add( ""+str[i]);
       row.add( ""+str[i+1]);
       row.add( ""+str[i+2]);
		return row;
		}
}

